package com.licheedev.myapplication

import android.app.Application
import android.content.Context
import android.os.Environment
import com.danikula.videocache.HttpProxyCacheServer
import com.kk.taurus.exoplayer.ExoMediaPlayer
import com.kk.taurus.ijkplayer.IjkPlayer
import com.kk.taurus.playerbase.config.PlayerConfig
import com.kk.taurus.playerbase.entity.DecoderPlan
import com.licheedev.adplayer.AdPlayerConfig
import com.licheedev.adplayer.loader.DefaultGlideImageLoader
import com.licheedev.adplayer.loader.DefaultVideoDownLoader
import com.licheedev.myapplication.AppKt
import tv.danmaku.ijk.media.player.IjkMediaPlayer
import java.io.File
import kotlin.concurrent.thread

class AppKt : Application() {
    override fun onCreate() {
        super.onCreate()
        sInstance = this
        AdPlayerConfig.instance()
            .init(this)
            // 开启播放实践log，默认false
            //.setEnableLog(true)
            // 可选，配置IjkPlayer播放器框架
            .configPlayerLibrary {
                thread {
                    IjkMediaPlayer.loadLibrariesOnce(null)
                    IjkMediaPlayer.native_setLogLevel(IjkMediaPlayer.IJK_LOG_SILENT)
                }
                
                PlayerConfig.addDecoderPlan(
                    DecoderPlan(1, IjkPlayer::class.java.name, "IjkPlayer")
                )
                PlayerConfig.setDefaultPlanId(1)
            }
            // 可选，配置ExoPlayer播放器框架
            .configPlayerLibrary {
                PlayerConfig.addDecoderPlan(
                    DecoderPlan(2, ExoMediaPlayer::class.java.name, "ExoPlayer")
                )
                PlayerConfig.setDefaultPlanId(2)
            }
            // 设置图片加载器（可选），默认使用Glide加载图片
            .setImageLoader(DefaultGlideImageLoader())
            // 设置视频缓存文件夹路径
            .setCacheDir(getAdCacheDir(this))
            // 设置视频文件下载器，仅使用setUseFileLruCache()有效
            .setVideoDownLoader(DefaultVideoDownLoader())
            // 下面两种缓存策略互斥，只有最后设置的才生效
            // 缓存策略1，先下载文件到Lru文件缓存里面，再播放
            .setUseFileLruCache(1024 * 1024 * 1024L)
            // 缓存策略2，使用AndroidVideoCache边下边播
            .setUseVideoCache { defaultCacheDir ->
                val builder =
                    HttpProxyCacheServer
                        .Builder(this@AppKt) // 这里也可以自定义缓存路径
                        .cacheDirectory(defaultCacheDir) // 缓存策略
                        .maxCacheSize(1024 * 1024 * 1024L) // 最大缓存文件总大小
                // .maxCacheFilesCount(10); // 或者用这个，最大缓存文件个数
                builder.build()
            }
    }

    private fun getAdCacheDir(context: Context): File {
        val state = Environment.getExternalStorageState()
        return if (state == Environment.MEDIA_MOUNTED) {
            // 已挂载
            val pic = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
            val dir = File(pic, "ad_cache")
            if (!dir.exists()) {
                dir.mkdirs()
            }
            dir
        } else {
            val cacheDir = context.cacheDir
            File(cacheDir, "ad_cache")
        }
    }

    companion object {
        var sInstance: AppKt? = null
    }
}